﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTQT;
using TTQT.TTQT_Code;
using MyWebUtils;
using System.IO;

public partial class YKienPhanHoi : MasterControl//System.Web.UI.UserControl
{
    //private void bindAnChi()
    //{
    //    dsMaAnChi.SelectCommandType = SqlDataSourceCommandType.Text;
    //    dsMaAnChi.SelectCommand =
    //        "select distinct Ma,tenanchi+ case(AnChiMau) when 1 then N' (mẫu)' when 0 then '' end [TenAnChi] from maanchi where MaDotPhatHanh=" +
    //        cboDotPhatHanh.SelectedValue;
    //    cboMaAnChi.DataSource = ((DataView)dsMaAnChi.Select(new DataSourceSelectArguments())).ToTable();
    //    cboMaAnChi.DataBind();
    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindGrid();
        }
    }
    public void bindGrid()
    {
        
        DataTable result = null;
        string sql =
            "select id,convert(varchar(10),date,103)+' '+convert(varchar(10),date,108) as date,email,fullname,address,phone,ip,parentid,detail,status,ordernum from ykienkhachhang";
        string whereClause = " where -1="+cboTrangThai.SelectedValue+" or status="+cboTrangThai.SelectedValue+" order by id desc";
       
        sql += whereClause;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand(sql, conn);

        comm.CommandType = CommandType.Text;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        detailGrid.DataSource= result;
        detailGrid.DataBind();

    }
    protected void detailGrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        MyWebUtil.goToElement(Page, "chiTietHoaDon");
        YKienKhachHangDAO sp = new YKienKhachHangDAO();
        try
        {
            if (e.CommandName == "cmdDelete")
            {
                sp.ID = e.Item.Cells[0].Text;
                
                if (sp.delete() > 0)
                {
                    
                    bindGrid();
                }
            }
            
            if (e.CommandName.Equals("cmdStatus"))
            {
                sp.yKienKhachHang_ToggleColumnValue("status", e.Item.Cells[0].Text, e.Item.Cells[8].Text);
               bindGrid();
            }
            
        }
        catch (SqlException ex)
        {
        }
    }
    protected void detailGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        try
        {
            if (e.Item.Cells[8].Text.Equals("1"))
            {
                e.Item.CssClass = "DarkRow";
            }
            else
            {
                e.Item.CssClass = "LightRow";
            }
        }
        catch { }
        try
        {
            ImageButton cmdLock = (ImageButton)e.Item.FindControl("cmdStatus");
            if (null != cmdLock)
            {
                if (e.Item.Cells[8].Text.Equals("0") || e.Item.Cells[8].Text.Equals("2"))
                {
                    cmdLock.ImageUrl = "../images/active0.png";
                }
                if (e.Item.Cells[8].Text.Equals("1"))
                {
                    cmdLock.ImageUrl = "../images/active1.png";
                }
            }
        }
        catch (Exception exx)
        {

        }
    }
    protected void detailGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        detailGrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }

    protected void cboTrangThai_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGrid();
    }
    protected void cmdRefresh_Click(object sender, EventArgs e)
    {
        bindGrid();
    }
}
